﻿/*
* This software is licensed under the GNU General Public License, version 2
* You may copy, distribute and modify the software as long as you track changes/dates of in source files and keep all modifications under GPL. You can distribute your application using a GPL library commercially, but you must also provide the source code.

* DNNspot Software (http://www.dnnspot.com)
* Copyright (C) 2013 Atriage Software LLC
* Authors: Kevin Southworth, Matthew Hall, Ryan Doom

* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

* Full license viewable here: http://www.gnu.org/licenses/gpl-2.0.txt
*/

using System;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DNNspot.Tabs.Common;
using DNNspot.Tabs.DataModel;

namespace DNNspot.Tabs
{
    public partial class Settings : ModuleSettingsBase
    {
        #region Events

        public override void LoadSettings()
        {
            if(!IsPostBack)
                LoadControls();
        }

        public override void UpdateSettings()
        {
            SaveSettings();
        }

        #endregion

        #region Functions

        private void LoadControls()
        {
            //ddlTemplates.DataSource = new Select().From<Template>().Where(Template.Columns.ModuleID).IsEqualTo(ModuleId).ExecuteTypedList<Template>();
            //ddlTemplates.DataTextField = Template.Columns.Title;
            //ddlTemplates.DataValueField = Template.Columns.Id;
            //ddlTemplates.DataBind();

            //ddlTemplates.Items.Insert(0, new ListItem("default", "default"));

            //if (ModuleSettings[ModuleSettingNames.TemplateId] != null && ddlTemplates.Items.FindByValue(Convert.ToString(ModuleSettings[ModuleSettingNames.TemplateId])) != null)
            //    ddlTemplates.Items.FindByValue(Convert.ToString(ModuleSettings[ModuleSettingNames.TemplateId])).Selected = true;

            if (ModuleSettings[ModuleSettingNames.Style] != null && ddlStyles.Items.FindByValue(Convert.ToString(ModuleSettings[ModuleSettingNames.Style])) != null)
                ddlStyles.Items.FindByValue(Convert.ToString(ModuleSettings[ModuleSettingNames.Style])).Selected = true;
            
            chkNoJQuery.Checked = !string.IsNullOrEmpty(HostSettings.GetHostSetting(ModuleSettingNames.NoJQuery));            
        }

        private void SaveSettings()
        {
            try
            {
                var hostSettingsController = new HostSettingsController();
                var moduleController = new ModuleController();

                //if (ddlTemplates.SelectedIndex != 0)
                //    moduleController.UpdateModuleSetting(ModuleId, ModuleSettingNames.TemplateId, ddlTemplates.SelectedValue);
                //else
                //    moduleController.DeleteModuleSetting(ModuleId, ModuleSettingNames.TemplateId);

                if (ddlStyles.SelectedIndex != 0)
                    moduleController.UpdateModuleSetting(ModuleId, ModuleSettingNames.Style, ddlStyles.SelectedValue);
                else
                    moduleController.DeleteModuleSetting(ModuleId, ModuleSettingNames.Style);

                if (chkNoJQuery.Checked)
                    hostSettingsController.UpdateHostSetting(ModuleSettingNames.NoJQuery, chkNoJQuery.Checked.ToString());
                else
                    hostSettingsController.UpdateHostSetting(ModuleSettingNames.NoJQuery, string.Empty);

                ModuleController.SynchronizeModule(ModuleId);
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        #endregion
    }

    public static class ModuleSettingNames
    {
        public const string Style = "Style";
        public const string TemplateId = "TemplateId";

        //public const string SerialNumber = "DNNspot_Tabs_SerialNumber";
        public const string NoJQuery = "DNNspot_Tabs_NoJQuery";
    }
}